﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms.Design;
using Win32Utils;

namespace CodeLibrary.Controls.PropertyGrid.PropertyGridEditors
{
    public class ConnectionStringEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                PropertyInfo oPropertyInfo = context.Instance.GetType().GetProperty("ConnectionString");
                string strInit = "";
                if (oPropertyInfo != null)
                    strInit = oPropertyInfo.GetValue(context.Instance, null).ToString();

                value = Win32APIProvider.PromptDataSource(strInit);
            }
            return value ?? "";
        }
    }
}
